package com.example.jasypt.component;

import com.ulisesbocchio.jasyptspringboot.util.Singleton;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;

/**
 * <p>
 *
 * </p>
 *
 * @author 王令
 * @since 2022/9/2 21:40
 */
@Slf4j
public class DefaultLazyEncryptor implements StringEncryptor {

    private final Singleton<StringEncryptor> singleton;

    public DefaultLazyEncryptor(String password) {
        singleton = new Singleton<>(() -> createDefault(password));
    }

    private StringEncryptor createDefault(String password) {
        return new StringEncryptorBuilder(password).build();
    }

    @Override
    public String encrypt(final String message) {
        return singleton.get().encrypt(message);
    }

    @Override
    public String decrypt(final String encryptedMessage) {
        return singleton.get().decrypt(encryptedMessage);
    }

}
